﻿using NewAndong.Entity;
using NewAndong.Service;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using AnDongAPI.Entity;

namespace NewAndong
{
    public class CacheHelper
    {
        private readonly IMemoryCache _memoryCache;
        private static readonly object _lockObject = new object();
        private SignalLampDirectionService signalLampDirectionService;
        private UnpackingDirectionService _unpackingDirectionService;
        public CacheHelper(IMemoryCache memoryCache)
        {
            _memoryCache = memoryCache;
            signalLampDirectionService = new SignalLampDirectionService();
            _unpackingDirectionService = new UnpackingDirectionService();
        }

        public void AddToCache(string key, object value, TimeSpan timeSpan)
        {
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = timeSpan
            });
        }


       
        //public List<SignalLampDirectionEntity> GetCache(string key, TimeSpan timeSpan)
        //{
        //    lock (_lockObject)
        //    {
        //        if (!_memoryCache.TryGetValue(key, out List<SignalLampDirectionEntity> cacheEntry))
        //        {

        //            var signalLampDirectionList = signalLampDirectionService.GetList(new Model.SignalLampDirectionModel { });
        //            cacheEntry = signalLampDirectionList;

        //            _memoryCache.Set(key, cacheEntry, new MemoryCacheEntryOptions
        //            {
        //                AbsoluteExpirationRelativeToNow = timeSpan
        //            });
        //        }
        //        return cacheEntry;
        //    }

        //}
        //public List<LA_UnpackingDirectionEntity> GetUnPackingCache(string key, TimeSpan timeSpan)
        //{
        //    lock (_lockObject)
        //    {
        //        if (!_memoryCache.TryGetValue(key, out List<LA_UnpackingDirectionEntity> cacheEntry))
        //        {
        //            var unpackingList = _unpackingDirectionService.GetAll(new Model.LA_UnpackingDirectionModel { });
        //            cacheEntry = unpackingList;

        //            _memoryCache.Set(key, cacheEntry, new MemoryCacheEntryOptions
        //            {
        //                AbsoluteExpirationRelativeToNow = timeSpan
        //            });
        //        }
        //        return cacheEntry;
        //    }
        //}
    }
}